import { createRouter, createWebHistory } from "vue-router";
import Home from "views/Home/index.vue";

const routes = [
    {
        path: "/home",
        redirect: "/",
    },
    {
        path: "/moviesinfo/:id",
        name: "MoviesInfo",
        meta: {
            keepAlive: false, //设置页面是否需要使用缓存
        },
        component: () => import("views/Home/Hot/MoviesInfo/index.vue"),
    },
    {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/",
                redirect: "hot",
            },
            {
                path: "adress",
                name: "Adress",
                meta: {
                    keepAlive: false, //设置页面是否需要使用缓存
                },
                component: () => import("views/Home/Adress/index.vue"),
            },
            {
                path: "hot",
                name: "Hot",
                meta: {
                    keepAlive: true, //设置页面是否需要使用缓存
                },
                component: () => import("views/Home/Hot/index.vue"),
            },
            {
                path: "cinema",
                name: "Cinema",
                meta: {
                    keepAlive: false, //设置页面是否需要使用缓存
                },
                component: () => import("views/Home/Cinema/index.vue"),
            },
            {
                path: "shown",
                name: "Shown",
                meta: {
                    keepAlive: false, //设置页面是否需要使用缓存
                },
                component: () => import("views/Home/Shown/index.vue"),
            },
            {
                path: "classic",
                name: "Classic",
                meta: {
                    keepAlive: false, //设置页面是否需要使用缓存
                },
                component: () => import("views/Home/Classic/index.vue"),
            },
            {
                path: "search",
                name: "Search",
                meta: {
                    keepAlive: false, //设置页面是否需要使用缓存
                },
                component: () => import("views/Search/index.vue"),
            },
        ],
    },
    {
        path: "/videos",
        name: "Videos",
        meta: {
            keepAlive: false, //设置页面是否需要使用缓存
        },
        component: () => import("views/Videos/index.vue"),
    },
    {
        path: "/shortvideos",
        name: "ShortVideos",
        meta: {
            keepAlive: false, //设置页面是否需要使用缓存
        },
        component: () => import("views/ShortVideos/index.vue"),
    },
    {
        path: "/show",
        name: "Show",
        meta: {
            keepAlive: false, //设置页面是否需要使用缓存
        },
        component: () => import("views/Show/index.vue"),
    },
    {
        path: "/profile",
        name: "Profile",
        meta: {
            keepAlive: false, //设置页面是否需要使用缓存
        },
        component: () => import("views/profile/index.vue"),
    },
    {
        path: "/login",
        name: "Login",
        meta: {
            keepAlive: false,
        },
        component: () => import("views/Login/index.vue"),
    },
    {
        path: "/:pathMatch(.*)*",
        component: () => import("views/404/index.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
});

// 全局路由钩子
router.beforeEach((to, from) => {
    let loginType = localStorage.getItem("loginType");
    console.log(localStorage.getItem("loginType"));
    if (to.path == "/profile") {
        if (loginType) {
            return true;
        }
        // 未登录
        return "/login";
    }
    return true;
});

export default router;
